package com.qf.servleProject.servlet.comtroller;

import com.qf.servleProject.entity.Manager;
import com.qf.servleProject.service.ManagerService;
import com.qf.servleProject.service.impl.ManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginMgrController", value = "/loginMgr")
public class LoginMgrController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1.处理乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
//        2. 收参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String inputVcode = request.getParameter("inputVcode");
//      对验证码进行验证
        String codes = (String) request.getSession().getAttribute("codes");
        if (!inputVcode.isEmpty() && inputVcode.equalsIgnoreCase(codes)) {
            //        3.调用业务方法
            ManagerService managerService = new ManagerServiceImpl();
            Manager mgr = managerService.login(username, password);
//        4.处理结果，流程跳转
            if (mgr != null) {
//            登录成功
//       将管理员信息存储到session 里
                HttpSession session = request.getSession();
                session.setAttribute("mgr", mgr);
//            跳转目标，方式
                response.sendRedirect("/WebProject_war_exploded/showallcontroller");
            } else {
                response.sendRedirect("/WebProject_war_exploded/loginMgr.html");
            }
        } else {
            response.sendRedirect("/WebProject_war_exploded/loginMgr.html");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
